package cn.tedu.rabbitmqspringboot.m2;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;

@Component
public class Producer {
    @Autowired
    private AmqpTemplate t;

    public void send() {
        while (true) {
            System.out.print("\n\n输入消息： ");
            String msg = new Scanner(System.in).nextLine();
            System.out.println(msg.length());
            // 默认是持久消息
            t.convertAndSend("task_queue", msg);

            // 设置成非持久消息，需要创建一个“消息预处理对象”
            // t.convertAndSend("task_queue", (Object) msg, new MessagePostProcessor() {
            //     @Override
            //     public Message postProcessMessage(Message message) throws AmqpException {
            //         MessageProperties p = message.getMessageProperties();
            //         p.setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
            //         return message;
            //     }
            // });



        }
    }
}
